package com.yipai.datewidget.booking.page;

import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.lifecycle.Observer;

import com.haibin.calendarview.CalendarUtil;
import com.haibin.calendarview.CalendarView;
import com.yipai.datewidget.booking.BookingSharedViewModel;
import com.yipai.datewidget.booking.R;
import com.yipai.datewidget.booking.base.BaseFragment;
import com.yipai.datewidget.booking.config.Config;
import com.yipai.datewidget.booking.databinding.FragmentCalendarBinding;
import com.yipai.datewidget.booking.model.DateRangeModel;

import java.util.Calendar;
import java.util.Date;

public class CalendarFragment extends BaseFragment {

    public static CalendarFragment newInstance(int index) {
        Bundle args = new Bundle();
        CalendarFragment fragment = new CalendarFragment();
        args.putInt(Config.KEY_INDEX, index);
        fragment.setArguments(args);
        return fragment;
    }

    public static final int MAX_YEAR = 2050;
    public static final int MAX_YEAR_MONTH = 12;
    public static final int MAX_YEAR_DAY = 31;

    private int mPageIndex;
    private DateRangeModel mDateRangeModel;
    private FragmentCalendarBinding mFragmentCalendarBinding;
    private BookingSharedViewModel mBookingSharedViewModel;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        mFragmentCalendarBinding = FragmentCalendarBinding.inflate(inflater, container, true);
        initData();
        initUi();
        initObserver();
        return mFragmentCalendarBinding.getRoot();
    }

    private void initObserver() {
        mBookingSharedViewModel.mDayDateMutableLiveData.observe(getViewLifecycleOwner(), new Observer<Date>() {
            @Override
            public void onChanged(Date date) {
                if(date != null){
                    Calendar calendar = Calendar.getInstance();
                    calendar.setTime(date);
                    mFragmentCalendarBinding.cvCalendar.scrollToCalendar(calendar.get(Calendar.YEAR),
                            calendar.get(Calendar.MONTH) + 1, calendar.get(Calendar.DAY_OF_MONTH));
                }
            }
        });
    }

    private void initData() {
        mBookingSharedViewModel = getActivityScopeViewModel(BookingSharedViewModel.class);
        mPageIndex = getArguments().getInt(Config.KEY_INDEX);

        mDateRangeModel = new DateRangeModel();
        Calendar calendar = Calendar.getInstance();
        mDateRangeModel.setMinYear(calendar.get(Calendar.YEAR));
        mDateRangeModel.setMinMonth(calendar.get(Calendar.MONTH) + 1);
        mDateRangeModel.setMinDay(calendar.get(Calendar.DAY_OF_MONTH));
        mDateRangeModel.setMaxYear(MAX_YEAR);
        mDateRangeModel.setMaxYearMonth(MAX_YEAR_MONTH);
        mDateRangeModel.setMaxYearDay(MAX_YEAR_DAY);
    }

    private void initUi() {
//        mFragmentCalendarBinding.cvCalendar.setMonthViewScrollable(false);
        mFragmentCalendarBinding.cvCalendar.setRange(mDateRangeModel.getMinYear(), mDateRangeModel.getMinMonth(),
                mDateRangeModel.getMinDay(), mDateRangeModel.getMaxYear(), mDateRangeModel.getMaxYearMonth(), mDateRangeModel.getMaxYearDay());
        // 默认隐藏向前翻月的图标
        mFragmentCalendarBinding.ivPrevious.setVisibility(View.GONE);

        mFragmentCalendarBinding.tvYearMonth.setText(getString(R.string.year_month, String.valueOf(mDateRangeModel.getMinYear()),
                String.valueOf(mDateRangeModel.getMinMonth())));

        mFragmentCalendarBinding.cvCalendar.setOnMonthChangeListener(new CalendarView.OnMonthChangeListener() {
            @Override
            public void onMonthChange(int year, int month) {
                if (year > mDateRangeModel.getMinYear()) {
                    mFragmentCalendarBinding.ivPrevious.setVisibility(View.VISIBLE);
                } else if (year == mDateRangeModel.getMinYear()) {
                    if (month > mDateRangeModel.getMinMonth()) {
                        mFragmentCalendarBinding.ivPrevious.setVisibility(View.VISIBLE);
                    } else {
                        mFragmentCalendarBinding.ivPrevious.setVisibility(View.GONE);
                    }
                } else {
                    mFragmentCalendarBinding.ivPrevious.setVisibility(View.GONE);
                }

                if (year < mDateRangeModel.getMaxYear()) {
                    mFragmentCalendarBinding.ivNext.setVisibility(View.VISIBLE);
                } else if (year == mDateRangeModel.getMaxYear()) {
                    if (month < mDateRangeModel.getMaxYearMonth()) {
                        mFragmentCalendarBinding.ivNext.setVisibility(View.VISIBLE);
                    } else {
                        mFragmentCalendarBinding.ivNext.setVisibility(View.GONE);
                    }
                } else {
                    mFragmentCalendarBinding.ivNext.setVisibility(View.GONE);
                }

                mFragmentCalendarBinding.tvYearMonth.setText(getString(R.string.year_month, String.valueOf(year),
                        String.valueOf(month)));
            }
        });
        mFragmentCalendarBinding.ivNext.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mFragmentCalendarBinding.cvCalendar.scrollToNext(true);
            }
        });
        mFragmentCalendarBinding.ivPrevious.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mFragmentCalendarBinding.cvCalendar.scrollToPre(true);
            }
        });

        mFragmentCalendarBinding.tvNextStep.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                setValue();
                mBookingSharedViewModel.mNextStepUnPeekLiveData.setValue(mPageIndex);
            }
        });
    }

    private void setValue(){
        com.haibin.calendarview.Calendar calendar = mFragmentCalendarBinding.cvCalendar.getSelectedCalendar();
        mBookingSharedViewModel.mDayDateMutableLiveData.setValue(new Date(calendar.getTimeInMillis()));
    }

    @Override
    public void onEvent() {
        super.onEvent();

        setValue();
    }
}
